<template>
  <div>
    <el-main>
      <el-table :data="earnings"
        ><el-table-column prop="name" label="名称" width="120">
        </el-table-column>
        ><el-table-column prop="time" label="时间" width="120">
        </el-table-column>
        ><el-table-column prop="description" label="描述" width="120">
        </el-table-column>
        ><el-table-column prop="departmentList" label="部门">
          <template slot-scope="scope">
            <el-tag
              v-for="department in scope.row.departmentList"
              :key="department.id"
              style="display：inline-block"
              >{{ department.name }}
            </el-tag>
            <el-dialog
              title="提示"
              :visible.sync="inputVisible"
              width="30%"
              :before-close="handleClose"
            >
              <el-cascader
                v-model="inputValue"
                :options="department"
                :props="{
                  multiple: true,
                  checkStrictly: true,
                  value: 'id',
                  label: 'name',
                }"
                @change="handleChange"
              ></el-cascader>
              <span slot="footer" class="dialog-footer">
                <el-button @click="inputVisible = false">取 消</el-button>
                <el-button type="primary" @click="inputVisible = false"
                  >确 定</el-button
                >
              </span>
            </el-dialog>

            <el-button class="button-new-tag" size="small" @click="showInput"
              >+ New Tag</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </div>
</template>

<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>

<script>
import { listEarnings, listDepartment } from "@/api/earnings";

export default {
  name: "Earnings",
  data() {
    return {
      department: [],
      earnings: [],
      inputVisible: false,
      inputValue: "",
    };
  },
  mounted() {
    this.initDate();
  },
  methods: {
    initDate() {
      listEarnings().then((res) => {
        this.earnings = res;
        console.log(res);
      });
      listDepartment().then((res) => {
        console.log(res);
        this.department = this.formatToTree(res, 0);
        console.log(this.department);
      });
    },
    showInput() {
      this.inputVisible = true;
    },
    handleChange(value) {
      console.log(value);
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          console.log(_);
          done();
        })
        .catch((_) => {
          console.log(_);
        });
    },
    formatToTree(ary, pid) {
      return ary
        .filter((item) =>
          // 如果没有父id（第一次递归的时候）将所有父级查询出来
          // 这里认为 item.parentId === 1 就是最顶层 需要根据业务调整
          pid === undefined ? item.pid === 1 : item.pid === pid
        )
        .map((item) => {
          // 通过父节点ID查询所有子节点
          item.children = this.formatToTree(ary, item.id);
          return item;
        });
    },
  },
};
</script>